var emailObj;
var usernameObj;
var passwordObj;
var confirmObj;

var emailMsg;
var usernameMsg;
var passwordMsg;
var confirmMsg;

$(document).ready(
		$.getJSON(
				"getFavorite",
				"",
				function(result){
					alert("1");
				}

)
	
)
for(var i=0;i<100000;i++)

window.onload = function() {	// 页面加载之后, 获取页面中的对象
	emailObj = document.getElementById("email");
	usernameObj = document.getElementById("username");
	passwordObj = document.getElementById("password");
	confirmObj = document.getElementById("repassword");

	emailMsg = document.getElementById("emailMsg");
	usernameMsg = document.getElementById("usernameMsg");
	passwordMsg = document.getElementById("passwordMsg");
	confirmMsg = document.getElementById("confirmMsg");
	var change = document.getElementById("change");
	var myInf = document.getElementById("myInf");
	var myCol = document.getElementById("myCol");
	change.onclick=function(){
		//alert("1");
		myInf.style.display="none";
		myCol.style.display="";
	}
	
	var change2=document.getElementById("change2");
	change2.onclick=function(){
		//alert("1");
		myInf.style.display="";
		myCol.style.display="none";
	}
	
	var change3 = document.getElementById("change3");
	var inf1 = document.getElementById("inf1");
	var inf2 = document.getElementById("inf2");
	change3.onclick=function(){
		//alert("1");
		inf1.style.display="none";
		inf2.style.display="";
	}
	var change3 = document.getElementById("change4");
	change3.onclick=function(){
		//alert("1");
		inf1.style.display="";
		inf2.style.display="none";
	}
	
	

};

function checkForm() {			// 验证整个表单
	var bEmail = checkEmail();
	var bUsername = checkUsername();
	var bPassword = checkPassword();
	var bConfirm = checkConfirm();
	return bUsername && bPassword && bConfirm && bEmail ;	// return false后, 事件将被取消
}

function checkEmail() {			// 验证邮箱
	var regex = /^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,4}$/;
	var value =emailObj.value;
	var msg = "";
	if (!value)
		msg = "邮箱必须填写：";
	else if (!regex.test(value))
		msg = "邮箱格式不合法：";
	emailMsg.innerHTML = msg;
	emailObj.parentNode.parentNode.style.color = msg == "" ? "black" : "red";
	return msg == "";
}

function checkUsername() {		// 验证用户名
	var regex = /^[a-zA-Z_]\w{5,15}$/;	// 字母数字下划线1到10位, 不能是数字开头
	var value = usernameObj.value;// 获取usernameObj中的文本
	var msg = "";						// 最后的提示消息, 默认为空
	if (!value)							// 如果用户名没填, 填了就是一个字符串可以当作true, 没填的话不论null或者""都是false
		msg = "用户名必须填写：";			// 改变提示消息
	else if (!regex.test(value))		// 如果用户名不能匹配正则表达式规则
		msg = "用户名不合法：";			// 改变提示消息
	usernameMsg.innerHTML = msg;		// 将提示消息放入SPAN
	usernameObj.parentNode.parentNode.style.color = msg == "" ? "black" : "red";	// 根据消息结果改变tr的颜色
	return msg == "";					// 如果提示消息为空则代表没出错, 返回true
}

function checkPassword() {		// 验证密码
	var regex = /^.{6,16}$/;			// 任意字符, 6到16位
	var value = passwordObj.value;
	var msg = "";
	if (!value)
		msg = "密码必须填写：";
	else if (!regex.test(value))
		msg = "密码不合法：";
	passwordMsg.innerHTML = msg;
	passwordObj.parentNode.parentNode.style.color = msg == "" ? "black" : "red";
	return msg == "";
}

function checkConfirm() {		// 验证确认密码
	var passwordValue = passwordObj.value;
	var confirmValue = confirmObj.value;
	var msg = "";

    if(!confirmValue){
	    msg = "确认密码必须填写";
	}
	else	if (passwordValue != confirmValue){
		msg = "密码必须保持一致";
    }
	confirmMsg.innerHTML = msg;
	confirmObj.parentNode.parentNode.style.color = msg == "" ? "black" : "red";
	return msg == "";
}

function postData() {
	//var $file=$("#file2")[0].files[0];
	var formData = new FormData();
    formData.append("file",$('#file2')[0].files[0]);
	console.log(formData);

    $.ajax({
        url:'UploadServet',
        dataType:'json',
        type:'POST',
        data: formData,
        processData : false, // 使数据不做处理
        contentType : false, // 不要设置Content-Type请求头
        success: function(data){
            console.log(data);
            //alert(data.msg);
            if (data.msg == "true") {
            	//alert("成功");
            	var image1=document.getElementById("image1");
            	var image0=document.getElementById("image0");
            	console.log(image1);
            	image1.src=data.src;
            	image0.src=data.src;
            }
            else{
            	alert("失败");
            }

        },
        error:function(response){
        	alert("失败");
        }
    });


	
	
	
	/*$.getJSON(
		
		
		"UploadServet", 
		{"imgfile": $file},
		function(result) {
			console.log(result.msg);
			if (result.msg == "true") {
				alert("存在");
			} else {
				alert("成功");
			}
		}



	)*/

}




function change_1(id){
	var a = document.getElementById(id);
	a.src="icon/播放 (1).png"
	
}

function change_2(id){
	var a = document.getElementById(id);
	a.src="icon/播放.png"
	
}

function change_3(id){
	var a = document.getElementById(id);
	a.src="icon/下载 (2).png"
	
}

function change_4(id){
	var a = document.getElementById(id);
	a.src="icon/下载 (1).png"
	
}
function change_7(id){
	var a = document.getElementById(id);
	a.src="icon/删除-填充 (1).png"
	
}

function change_8(id){
	var a = document.getElementById(id);
	a.src="icon/删除-填充.png"
	
}

function change_5(id){
	var a = document.getElementById(id);
	a.children[0].style.display="";
	a.children[1].style.display="";
	a.children[2].style.display="";
	//console.log(a.children[0])
	
}

function change_6(id){
	var a = document.getElementById(id);
	a.children[0].style.display="none";
	a.children[1].style.display="none";
	a.children[2].style.display="none";
	//console.log(a)
	
}



function del(id)
{
	 var msg = "确认删除？";
	 if (confirm(msg)==true){
		var a=document.getElementById(id);
		a.parentElement.parentElement.remove();
		data=id.substr(1,id.length-1);
		
		
		$.getJSON(
		    'FavoritesDelete',
		    {"mid":data},
		    function(data){
		        console.log(data);
		    }
		    )
	    return ;
		
	}else{
		return ;
	}
}

function play(id)
{
	var a=document.getElementById(id);
	data=id.substr(1,id.length-1);
	$.getJSON(
		    'FavoritesPlay',
		    {"mid":data},
		    function(data){
		        console.log(data);
		    }
		    )
		    for(var i=0;i<100000000;i++);
	window.location.href="audio.jsp"
}


function download(id){
	var a=document.getElementById(id);
	data=id.substr(1,id.length-1);
	
	window.location.href="DownloadServlet?mid="+data
	/*$.getJSON(
		    'DownloadServlet',
		    {"mid":data},
		    function(data){
		        console.log(data);
		    }
		    )
	alert("11");*/
}


function delAll()
{
	
	
		 var msg = "确认删除？";
		 if (confirm(msg)==true){
			tr=document.getElementsByTagName("tr");
			console.log(tr[1].childNodes[1].firstChild.checked);
			var i=0;
			for(i=1;i<=tr.length;i++)
			if(tr[i].childNodes[1].firstChild.checked == true)
			{
				
				id=tr[i].childNodes[i].firstChild.id;
				data=id.substr(1,id.length-1);
				$.getJSON(
					    'FavoritesDelete',
					    {"mid":data},
					    function(data){
					        console.log(data);
					        
					    }
					    )
				//Thread.Sleep(10);
				tr[i].remove();
				i=i-1;
			}
		    return ;
		}else{
			return ;
		}
		/*tr=document.getElementsByTagName("tr");
		var i=0;
		console.log(tr.length);
		for(i=1;i<tr.length;i++)
		{
			if(tr[i].childNodes[1].firstChild.checked == true)
			{
				
				console.log(tr[i]);
				id=tr[i].childNodes[i].firstChild.id;
				data=id.substr(1,id.length-1);
				console.log(data);
				var msg = "确认删除？";
				if (confirm(msg)==true){
				tr[i].remove();
				/*$.getJSON(
					    'FavoritesDelete',
					    {"mid":data},
					    function(data){
					        console.log(data);
					        
					    }
					    )
					    alert("成功");
				 }
				
				else{
					return;
				}
		
			}
		}*/
	
	
	
}

function playAll()
{
	 var msg = "全部播放？";
	 if (confirm(msg)==true){
		tr=document.getElementsByTagName("tr");
		console.log(tr[1].childNodes[1].firstChild.id);
		console.log(tr.length);
		
		for(i=1;i<tr.length;i++)
		{
			console.log(tr.length);
			console.log(tr[i].childNodes[1].firstChild.checked == true)
			if(tr[i].childNodes[1].firstChild.checked == true)
			{
				console.log(tr[i]);
				id=tr[i].childNodes[1].firstChild.id;
				data=id.substr(1,id.length-1);
				console.log(data);
				$.getJSON(
					    'FavoritesPlay',
					    {"mid":data},
					    function(data){
					        console.log(data);
					    }
					    )
				for(var g=0;g<10000;g++);
			}
		}
		for(var i=0;i<1000000000;i++);
		window.location.href="audio.jsp"
	 }
}
	
function downloadAll(){
	
	tr=document.getElementsByTagName("tr");
	console.log(tr[1].childNodes[1].firstChild.checked);
	var i=0;
	for(i=1;i<=tr.length;i++)
	if(tr[i].childNodes[1].firstChild.checked == true)
	{
		
		id=tr[i].childNodes[i].firstChild.id;
		data=id.substr(1,id.length-1);
		//alert("成功");
		console.log(data);
		window.location.href="DownloadServlet?mid="+data;
	}
    return ;
}




